//
//  SY_ColumnChartView.m
//  SY_ ColumnChartView
//
//  Created by 苏楠 on 16/8/15.
//  Copyright © 2016年 苏楠. All rights reserved.
//

#import "SY_ColumnChartView.h"

@interface SY_ColumnChartView ()

//@property (nonatomic, strong) 

@end

@implementation SY_ColumnChartView
{
    NSArray *_itemArr;
    //NSInteger maxCount;
}


- (instancetype)initWithFrame:(CGRect)frame items:(NSArray *)itemArr{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        _itemArr = itemArr;
        
        
        [self setUpView];
        
    }
    return self;
}

- (void)setUpView {
    
    NSMutableArray *countArr = [[NSMutableArray alloc]init];
    for (NSDictionary * dic in _itemArr) {
        [countArr addObject:[dic objectForKey:@"usernum"]];
    }
    
    NSInteger maxCount = [[countArr valueForKeyPath:@"@max.intValue"] integerValue];
    
    
    
    [self setBackgroundColor:[UIColor whiteColor]];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(49, self.frame.size.height - 22, 188, 1)];
    [line setBackgroundColor:RGBCOLOR(221,221,221)];
    [self addSubview:line];
    
    for (int i =0 ; i < [_itemArr count]; i ++) {
     
        NSDictionary *dic = [_itemArr objectAtIndex:i];
        
        float height = maxCount == 0?0:[[dic objectForKey:@"usernum"] floatValue]/maxCount * 108.0;
        
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(49+25+(17+38)*i, 108-height+14, 17, height)];
        NSArray *rgbArr = [[dic objectForKey:@"color"] componentsSeparatedByString:@","];
        [itemView setBackgroundColor:RGBCOLOR([rgbArr.firstObject integerValue], [[rgbArr objectAtIndex:1] integerValue], [rgbArr.lastObject integerValue])];
        [self addSubview:itemView];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, itemView.bottom + 10, 44, 10)];
        [titleLabel setFont:[UIFont systemFontOfSize:11]];
        titleLabel.text = [dic objectForKey:@"txt"];
        [titleLabel setTextColor:RGBCOLOR(59, 59, 59)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.centerX = itemView.centerX;
        //titleLabel.y = itemView.bottom - 10;
        [self addSubview:titleLabel];
        
        
    }
    
    UILabel *minLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, self.frame.size.height - 26, 20, 10)];
    minLabel.text = @"0人";
    [minLabel setFont:[UIFont systemFontOfSize:11]];
    [minLabel setTextColor:RGBCOLOR(59, 59, 59)];
    
    [self addSubview:minLabel];
    
    
    UILabel *maxLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 10, 20, 10)];
    maxLabel.text = [NSString stringWithFormat:@"%ld人",(long)maxCount];
    [maxLabel setFont:[UIFont systemFontOfSize:11]];
    [maxLabel setTextColor:RGBCOLOR(59, 59, 59)];
    [self addSubview:maxLabel];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(49, 14, 188, 0.5)];
    [line1 setBackgroundColor:RGBCOLOR(221,221,221)];
    [self addSubview:line1];
    
    NSMutableArray *countInfoArr = [[NSMutableArray alloc]init];
    [countInfoArr addObject:[NSString stringWithFormat:@"共计：%@",[countArr valueForKeyPath:@"@sum.intValue"]]];
    [countInfoArr addObject:[NSString stringWithFormat:@"病假：%@",[countArr objectAtIndex:0]]];
    [countInfoArr addObject:[NSString stringWithFormat:@"事假：%@",[countArr objectAtIndex:1]]];
    [countInfoArr addObject:[NSString stringWithFormat:@"其他请假：%@",[countArr objectAtIndex:2]]];
    
    for (int i = 0; i < [countInfoArr count]; i ++) {
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH -110, 44+i*21, 86, 13)];
        [label setFont:[UIFont systemFontOfSize:13]];
        label.text = [countInfoArr objectAtIndex:i];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = RGBCOLOR(59, 59, 59);
        
        [self addSubview:label];
    }
    
}

@end
